package controller

import (
	"ginfast/configs"
	"ginfast/pkg/ctx"
	"os"

	"github.com/gin-gonic/gin"
)

// UploadPicHandler 图片服务
func UploadPicHandler(c *gin.Context) {
	fileHead, err := c.FormFile("file")
	if err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	filename := configs.Public.Abs("upload", "picture", fileHead.Filename)
	os.MkdirAll(filename, os.ModePerm)
	if err := c.SaveUploadedFile(fileHead, filename); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWrite(gin.H{"name": filename, "url": filename}, c)
}
